﻿using System;
using System.Collections.Generic;
using NJabber.Client.StanzaFactories;

namespace NJabber.Client
{
    public class PresenceStatusStorage : IPresenceStatusProvider, IPresenceStatusUpdater
    {
        private Dictionary<JID, PresenceStatus> dataDictionary;

        public PresenceStatusStorage()
        {
            dataDictionary = new Dictionary<JID, PresenceStatus>();
        }

        public PresenceStatus this[JID jid] 
        { 
            get
            {
                if (!dataDictionary.ContainsKey(jid)) return new PresenceStatus(jid);
                return dataDictionary[jid];
            } 
        }

        public void UpdatePresence(JID entityJid, bool isAvailable, AvailabilityStatus status = AvailabilityStatus.None, string statusDescription = "")
        {
            if (entityJid == null) throw new ArgumentNullException("entityJid");
            dataDictionary[entityJid] = new PresenceStatus(entityJid, isAvailable, status, statusDescription);
        }
    }

    public class PresenceStatus
    {
        public PresenceStatus(JID entityJid, bool isAvailable = false, AvailabilityStatus status = AvailabilityStatus.None, string statusDescription = "")
        {
            EntityJid = entityJid;
            IsAvailable = isAvailable;
            AvailabilityStatus = status;
            AvailabilityDescription = statusDescription;
        }

        public JID EntityJid { get; internal set; }
        public bool IsAvailable { get; internal set; }
        public AvailabilityStatus AvailabilityStatus { get; internal set; }
        public string AvailabilityDescription { get; internal set; }

        public override bool Equals(object obj)
        {
            if (obj == null && !(obj is PresenceStatus)) return false;
            return Equals((PresenceStatus) obj);
        }

        public bool Equals(PresenceStatus data)
        {
            if (data == null) return false;
            return data.EntityJid.Equals(EntityJid) &&
                   data.IsAvailable == IsAvailable &&
                   data.AvailabilityStatus == AvailabilityStatus &&
                   data.AvailabilityDescription == AvailabilityDescription;
        }
    }
}
